package com.quanquan.user.service;

import com.quanquan.user.entity.TemplateId;
import com.quanquan.user.repository.TemplateIdRepository;
import com.quanquan.user.request.TemplateIdRequest;
import com.quanquan.user.service.weixin.MiniProgramProxy;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

/**
 * @ClassName:TemplateIdService
 * @Description:TODO
 * @Author:xiongjunbao
 * @Date:2020/4/14 14:25
 * @Version 1.0
 */
@Service
public class TemplateIdService {

    private final TemplateIdRepository templateIdRepository;
    private final MiniProgramProxy miniProgramProxy;

    @Autowired
    public TemplateIdService(TemplateIdRepository templateIdRepository, MiniProgramProxy miniProgramProxy) {
        this.templateIdRepository = templateIdRepository;
        this.miniProgramProxy = miniProgramProxy;
    }


    /**
     * 根据类型返回对应的模板id列表
     *
     * @return
     */
    public List<Object> findByType(TemplateIdRequest request) {
        List<Object> objectList = new LinkedList<>();
        List<TemplateId> templateIds = templateIdRepository.findByType(request.getType());
        for (TemplateId templateId : templateIds) {
            Map<String, Object> map = new HashMap<>();
            map.put("templateId", templateId.getTemplateId());
            map.put("name", templateId.getName());
            objectList.add(map);
        }
        return objectList;
    }

    /**
     * 订阅消息（报名成功通知，这个在报名成功后弹出授权提醒）
     *
     * @return
     */
    public boolean sendSignUpActivityMessage(TemplateIdRequest request) {
        return miniProgramProxy.sendSignUpActivityMessage(request.getUserId(), request.getActivityName(), request.getActivityTime(), request.getActivityType(), request.getCouponName(), request.getAddress());
    }

    /**
     * 订阅消息（注册成功）
     *
     * @return
     */
    public boolean sendRegMessage(TemplateIdRequest request) {
        return miniProgramProxy.sendRegMessage(request.getUserId());

    }

    /**
     * 订阅消息（会员积分变动）
     *
     * @return
     */
    public boolean sendIntegralChangeMessage(TemplateIdRequest request) {
        String reason;
        if (request.getType()==1){
            reason = "积分商城兑换";
        }else if (request.getType()==2){
            reason = "停车兑换车费";
        }else{
            reason = "消费加积分";
        }
        return miniProgramProxy.sendIntegralChangeMessage(request.getUserId(),request.getIntegral(),reason);
    }

    /**
     * 订阅消息（停车支付）
     *
     * @return
     */
    public boolean sendParkingFeeMessage(TemplateIdRequest request) {
        return miniProgramProxy.sendParkingFeeMessage(request.getUserId(),request.getFee());

    }

}
